﻿using System;
using sysConfig = System.Configuration;
using Microsoft.Practices.Unity;

namespace SharedGenomics.Core.Configuration
{
    /// <summary>
    /// The purpose of this class is to set up an IUnityContainer instance with all the mappings required for data access. It supports providing
    /// stub values if no config file is found. Subclasses should provide the specific Configuration instance to use.
    /// </summary>
    public abstract class ConfigReaderFactory
    {
        private readonly bool _provideSettings;

        /// <summary>
        /// Default constructor - does not attempt to provide any defaults values for settings
        /// </summary>
        public ConfigReaderFactory()
            : this(false)
        {
        }

        /// <summary>
        /// Creates a new instance which configures whether to provide default values for application settings (connection strings and data paths)
        /// </summary>
        /// <param name="provideSettings">Whether default settings should be provided</param>
        public ConfigReaderFactory(bool provideSettings)
        {
            this._provideSettings = provideSettings;
        }

        public virtual IConfigurationReader GetConfigurationReader()
        {
            return this._provideSettings ? this.DefaultConfigurationReader : this.GetConfiguration();
        }

        /// <summary>
        /// When overridden by a subclass, this should return an <see cref="IConfigurationReader"/> instance which contains the application settings. This reader 
        /// is expected to contain JobDatabaseConnectionString and SnpDatabaseConnectionString connection strings, along with dataRoot[External|Local|UNC] settings in the AppSettings section.
        /// </summary>
        /// <returns>An <see cref="IConfigurationReader"/> instance for the current configuration</returns>
        protected abstract IConfigurationReader GetConfiguration();

        public IConfigurationReader DefaultConfigurationReader { get; set; }
    }
}
